/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReservaAulesPanel.java
 *
 * Created on 21-nov-2011, 18:59:52
 */
package edu.uoc.tdp.pac4.only5.gui.client.panels.connexio;

import edu.uoc.tdp.pac4.only5.domain.Resource;
import edu.uoc.tdp.pac4.only5.domain.Space;
import edu.uoc.tdp.pac4.only5.exceptions.EespaisDatabaseException;
import edu.uoc.tdp.pac4.only5.gui.client.model.ResourceListModel;
import edu.uoc.tdp.pac4.only5.gui.client.model.SpaceTableModel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.AbstractPanel;
import edu.uoc.tdp.pac4.only5.manager.BookingManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.tools.ResourceBundleTool;
import java.rmi.RemoteException;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;

/**
 *
 * @author Gerard
 */
public class ConsultaAulesPanel extends AbstractPanel {
    
    private SpaceManager spaceManager;
    
    private BookingManager bookingManager;
    
    private ResourceManager resourceManager;
    
    private PaymentManager paymentManager;
    
    private List<Space> spaces;
    
    private ReservarAulaPanel panel;
    
    private JFrame jframe;
 
    /** Creates new form ReservaAulesPanel */
    public ConsultaAulesPanel(JFrame jframe, SpaceManager spaceManager, BookingManager bookingManager, ResourceManager resourceManager, PaymentManager paymentManager) {
        initComponents();
        this.spaceManager = spaceManager;
        this.bookingManager = bookingManager;
        this.resourceManager = resourceManager;
        this.paymentManager = paymentManager;
        fillComboCapacitat();
        fillComboLocalitat();
        fillComboPreuHora();
        fillComboPreuDia();
        fillListResources();
        this.jframe = jframe;
        setLabels();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblAulesSegonsCriteri = new javax.swing.JLabel();
        lblLocalitat = new javax.swing.JLabel();
        lblPreuHoraMinim = new javax.swing.JLabel();
        lblPreuDiaMinim = new javax.swing.JLabel();
        cmbCapacitat = new javax.swing.JComboBox();
        cmbLocalitat = new javax.swing.JComboBox();
        cmbPreuHora = new javax.swing.JComboBox();
        cmbPreuDia = new javax.swing.JComboBox();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstResources = new javax.swing.JList();
        lblRecursos = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblSpaces = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        lblReservarAulaTitle = new javax.swing.JLabel();
        lblCapacitat = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(800, 600));

        lblAulesSegonsCriteri.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        lblAulesSegonsCriteri.setText("Aules segons criteri:");
        lblAulesSegonsCriteri.setName("lblAulesSegonsCriteri"); // NOI18N

        lblLocalitat.setText("Localitat");
        lblLocalitat.setName("lblLocalitat"); // NOI18N

        lblPreuHoraMinim.setText("Preu Hora Minim");
        lblPreuHoraMinim.setName("lblPreuHoraMinim"); // NOI18N

        lblPreuDiaMinim.setText("Preu Dia Minim");
        lblPreuDiaMinim.setName("lblPreuDiaMinim"); // NOI18N

        cmbCapacitat.setName("cmbCapacitat"); // NOI18N

        cmbLocalitat.setName("cmbLocalitat"); // NOI18N

        cmbPreuHora.setName("cmbPreuHora"); // NOI18N

        cmbPreuDia.setName("cmbPreuDia"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lstResources.setName("lstResources"); // NOI18N
        jScrollPane1.setViewportView(lstResources);

        lblRecursos.setText("Recursos a l'aula:");
        lblRecursos.setName("lblRecursos"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblSpaces.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblSpaces.setName("tblSpaces"); // NOI18N
        tblSpaces.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSpacesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblSpaces);

        jButton1.setText("Buscar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblReservarAulaTitle.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        lblReservarAulaTitle.setText("Reservar aula");
        lblReservarAulaTitle.setName("lblReservarAulaTitle"); // NOI18N

        lblCapacitat.setText("Capacitat Minima");
        lblCapacitat.setName("lblCapacitat"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lblPreuHoraMinim)
                                            .addComponent(lblPreuDiaMinim))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(cmbPreuDia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(cmbPreuHora, 0, 181, Short.MAX_VALUE)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblCapacitat)
                                                .addComponent(lblLocalitat))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(cmbCapacitat, 0, 187, Short.MAX_VALUE)
                                                .addComponent(cmbLocalitat, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(lblRecursos)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(4, 4, 4)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblAulesSegonsCriteri)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 736, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(148, 148, 148))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblReservarAulaTitle)
                        .addContainerGap(1098, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 1056, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(157, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblReservarAulaTitle)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbLocalitat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAulesSegonsCriteri)
                    .addComponent(lblLocalitat))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbCapacitat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCapacitat))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPreuHoraMinim)
                            .addComponent(cmbPreuHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPreuDiaMinim)
                            .addComponent(cmbPreuDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRecursos)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Integer capacitat = Integer.parseInt(cmbCapacitat.getSelectedItem().toString());
        Double priceHour  = Double.parseDouble(cmbPreuHora.getSelectedItem().toString());
        Double priceDay   = Double.parseDouble(cmbPreuDia.getSelectedItem().toString());
        String locality = cmbLocalitat.getSelectedItem().toString();

        try {
            spaces = spaceManager.getSpaces(locality, capacitat, priceHour, priceDay, lstResources.getSelectedValues());
        } catch (RemoteException ex) {
            showErrorMessageDialog(this, "error.generic.rmi");
        } catch(EespaisDatabaseException ex){
            showErrorMessageDialog(this, "error.generic.databaseerror");
        } 
        
        tblSpaces.setModel(new SpaceTableModel(spaces, ResourceBundleTool.columnsTableSpaces));
        setTableHeaders(ResourceBundleTool.columnsTableSpaces, tblSpaces);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tblSpacesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSpacesMouseClicked
        if(evt.getClickCount() == 2){
                Space space = spaces.get(tblSpaces.getSelectedRow());
                panel = new ReservarAulaPanel(space, resourceManager, bookingManager, paymentManager);
                panel.setVisible(true);
                this.setVisible(false);
                jframe.add(panel);
                jframe.pack();                
        }
    }//GEN-LAST:event_tblSpacesMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbCapacitat;
    private javax.swing.JComboBox cmbLocalitat;
    private javax.swing.JComboBox cmbPreuDia;
    private javax.swing.JComboBox cmbPreuHora;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblAulesSegonsCriteri;
    private javax.swing.JLabel lblCapacitat;
    private javax.swing.JLabel lblLocalitat;
    private javax.swing.JLabel lblPreuDiaMinim;
    private javax.swing.JLabel lblPreuHoraMinim;
    private javax.swing.JLabel lblRecursos;
    private javax.swing.JLabel lblReservarAulaTitle;
    private javax.swing.JList lstResources;
    private javax.swing.JTable tblSpaces;
    // End of variables declaration//GEN-END:variables

    @Override
    protected void setLabels() {
        lblAulesSegonsCriteri.setText(getText("aulesSegonsCriteri"));
        lblCapacitat.setText(getText("capacitatMinima"));
        lblLocalitat.setText(getText("localitat"));
        lblPreuDiaMinim.setText(getText("preuDiaMinim"));
        lblPreuHoraMinim.setText(getText("preuHoraMinim"));
        lblRecursos.setText(getText("recursosAula"));
        lblReservarAulaTitle.setText(getText("reservarAulaTitle"));
    }

    private void fillComboCapacitat(){
        Integer[] capacitat = {0, 5, 10 ,15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 150, 200, 250, 300, 350, 400, 450, 500};
        cmbCapacitat.setModel(new DefaultComboBoxModel(capacitat));
    }
    
    private void fillComboLocalitat(){
        try {
            List<String> localitats = spaceManager.getLocationsOfSpaces();
            cmbLocalitat.setModel(new javax.swing.DefaultComboBoxModel(localitats.toArray()));
        } catch (RemoteException ex) {
            showErrorMessageDialog(this, "error.generic.rmi");
        } catch(EespaisDatabaseException ex){
            showErrorMessageDialog(this, "error.generic.databaseerror");
        }                        
    }
    
    private void fillComboPreuHora(){
        Integer[] preu = {0, 5, 10 ,15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 150, 200, 250, 300, 350, 400, 450, 500};
        cmbPreuHora.setModel(new javax.swing.DefaultComboBoxModel(preu));
    }
    
    private void fillComboPreuDia(){
        Integer[] preu = {0, 5, 10 ,15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 150, 200, 250, 300, 350, 400, 450, 500, 1000, 2000, 3000, 4000, 5000};
        cmbPreuDia.setModel(new javax.swing.DefaultComboBoxModel(preu));
    }
    
    private void fillListResources(){
        try {
            List<Resource> resources = spaceManager.getAllResourcesOfAllSpaces();
            lstResources.setModel(new ResourceListModel(resources));
        } catch (RemoteException ex) {
            showErrorMessageDialog(this, "error.generic.rmi");
        } catch(EespaisDatabaseException ex){
            showErrorMessageDialog(this, "error.generic.databaseerror");
        } 

    }
}
